package com.googlecode.animator.controller
{
	
	import com.googlecode.data.Notifications;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
		
	/**
	 * Responds to user saving the project. 
	 * @author USER
	 * 
	 */
	public class SaveProjectCommand extends BaseCommand
	{
		/**
		 * Executes command. 
		 * @param notification
		 * 
		 */
		override public function execute(notification:INotification):void
		{
			super.execute(notification);
			
			var file:File = new File();
			var fileStream:FileStream = new FileStream();
			
			if(notification.getBody())
			{		
				settings.savePath = notification.getBody() as String;
			}
			
			file.url = settings.savePath;
			
			if(file.extension != "xml")
			{
				file.url = settings.savePath + ".xml";
			}
			
			try
			{
				fileStream.open(file, FileMode.WRITE);
				fileStream.writeUTFBytes(settings.getProjectXML().toXMLString());
				
			}catch(err:Error)
			{
				trace("Saving project.xml \n" + err.message);
				Alert.show("project.xml in use. Close and try again.");
			}
			
			fileStream.close();	
			
			sendNotification(Notifications.PROJECT_SAVED);
		}
	}
}